Imports System.Data
Imports System.Data.SqlClient

Public Class PackageList

    Private Sub PackageList_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        LoadIcon(Me)
        refreshGrid()



    End Sub

    Private Sub refreshGrid()

        Dim str As String
        str = "SELECT * FROM Packages WHERE Status = 1 ORDER BY PackageName"
        Dim cmd As New SqlCommand(str, gCnnPCE)
        Dim da As New SqlDataAdapter(cmd)
        Dim ds As New DataSet
        da.Fill(ds, "Packages")
        Me.GridControl1.DataSource = ds.Tables("Packages")

    End Sub

    Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click
        Me.Close()
    End Sub

    Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
        Dim frm As New CreatePackage
        frm.Action = "ADD"
        frm.ShowDialog()
        refreshGrid()
    End Sub

    Private Sub btnEdit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEdit.Click
        If GridView1.RowCount = 0 Then
            Exit Sub
        End If

        If GridView1.RowCount = 0 Then
            Exit Sub
        End If
        Dim i As Integer = ArrayToString(GridView1.GetSelectedRows)
        Dim SelectedID As Integer = GridView1.GetRowCellValue(i, GridView1.Columns("ID"))

        Dim frm As New CreatePackage
        frm.Action = "EDIT"
        frm.ID = SelectedID
        frm.ShowDialog()
        refreshGrid()

    End Sub

    Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click
        If GridView1.RowCount = 0 Then
            Exit Sub
        End If

        Dim i As Integer = ArrayToString(GridView1.GetSelectedRows)
        Dim SelectedID As Integer = GridView1.GetRowCellValue(i, GridView1.Columns("ID"))

        Dim str As String
        str = "DELETE  FROM Packages WHERE ID = " & SelectedID
        Dim cmd As New SqlCommand(str, gCnnPCE)
        If cmd.ExecuteNonQuery > 0 Then
            MsgBox("Delete Successfully", MsgBoxStyle.Information)
            Me.Close()
        Else
            MsgBox("Fail to Delete", MsgBoxStyle.Exclamation)
        End If



    End Sub
End Class